#单行文本数据集
#用来测试汉王文本行识别的准确度
#在线测试，需要公司内网
import os
from common.get_hw_pre import getresult
from recongnize import  damerau_levenshtein_distance
filenames = []
anns = []
num_label = 0
num_chars = 0
dir = "E:\internship\date_test\8.25\crop_imgfby"

# ==================================================================================
for line in open(os.path.join(dir,"Label.txt"), "r", encoding='utf-8'):
    strs = line.split("\t")
    filename = os.path.join(dir, strs[0])
    filenames.append(filename)
    anns.append(strs[1])

sample_metrics = []
from tqdm import tqdm
for i, fn in enumerate(tqdm(filenames)):
    label = anns[i]
    # pre:
    pre_chars = []
    pre_boxes = []
    pre = getresult(fn)
    paraphs = []
    if "Paraph" in pre.keys():
        paraphs = pre["Paraph"]
    all_pre_lines = []

    for para in paraphs:
        lines = para["line"]
        all_pre_lines = all_pre_lines + lines
        for line in lines:
            pre_chars.append(line["code"])
            coords = line["coords"]
            pre_boxes.append(coords)
    if len(pre_chars) > 1:
        print("error")
        continue
    damerau_levenshtein_distance(pre_chars, label)

# true_positives, pred_scores, recong_dist, recong_dist_no_del, len_chars = [np.concatenate(x, 0) for x in list(zip(*sample_metrics))]
#
# percision, recall = getpr(true_positives, num_label)
# f_measure = (2 * percision * recall) / percision * recall
# sum_len_chars = sum(len_chars)
# sum_dist = sum(recong_dist)
# CAR = (sum_len_chars - sum_dist) / sum_len_chars
#
# sum_dist2 = sum(recong_dist_no_del)
# CAR_no_del = (sum_len_chars - sum_dist2) / sum_len_chars
# pass
